﻿
Public Class SpriteFactory
    Private _Factory As ResourceFactory

    Protected Friend Sub New(ownerFactory As ResourceFactory)
        _Factory = ownerFactory
    End Sub

    Public Function CreateSprites(Of T As Sprite)(definitionFileAsset As String) As List(Of T)
        Dim results As New List(Of T)
        Dim fileLines() As String = _Factory.Engine.Resources.GetDefinitionFile(definitionFileAsset)
        Dim index As Integer = 0
        Dim lineList As New List(Of String)
        While index < fileLines.Length
            Dim line As String = fileLines(index)
            If line.Length > 0 Then
                lineList.Add(line)
            Else
                Dim result As T = ParseSprite(Of T)(lineList.ToArray)
                If result IsNot Nothing Then results.Add(result)
                lineList.Clear()
            End If
            index += 1
        End While
        Return results
    End Function

    Public Function CreateSprite(Of T As Sprite)(spriteName As String, imageResource As String, frameBounds As Rectangle) As T
        Dim result As T = Sprite.CreateNew(spriteName, imageResource, frameBounds)
        Return result
    End Function

    Protected Overridable Function ParseSprite(Of T As Sprite)(ParamArray fileLines() As String) As T
        If fileLines.Length > 0 Then
            If fileLines.Length = 1 AndAlso fileLines(0).Contains(Microsoft.VisualBasic.ControlChars.NewLine) Then
                fileLines = fileLines(0).Split(Microsoft.VisualBasic.ControlChars.NewLine)
            End If
            If fileLines.Length > 2 Then
                Dim spriteName As String = fileLines(0)
                Dim ImageSheet As String = fileLines(1)
                Dim frameValues() As String = fileLines(2).Split(" "c)
                Dim frameX, frameY, frameWidth, frameHeight As Integer
                If frameValues.Length = 4 Then
                    If Integer.TryParse(frameValues(0), frameX) Then
                        If Integer.TryParse(frameValues(1), frameY) Then
                            If Integer.TryParse(frameValues(2), frameWidth) Then
                                If Integer.TryParse(frameValues(3), frameHeight) Then
                                    Dim result As T = CreateSprite(Of T)(spriteName, ImageSheet, New Rectangle(frameX, frameY, frameWidth, frameHeight))
                                    Dim typeInfo As Type = GetType(T)
                                    For index As Integer = 3 To fileLines.Length - 1
                                        Dim lineValues() As String = fileLines(index).Split("=")
                                        If lineValues.Length = 2 Then
                                            Dim info As System.Reflection.PropertyInfo = typeInfo.GetProperty(lineValues(0).Trim)
                                            If info IsNot Nothing Then
                                                ResourceFactory.SetPropertyValueFromString(info, result, lineValues(1))
                                            End If
                                        End If
                                    Next
                                    Return result
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
        Return Nothing
    End Function
End Class
